package vue;

import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import model.EErrorPopUp;
import model.session.CNFUse;
import controller.IController;
import controller.ISessionManager;

/**
 * Implementation of {@link IIG}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IG extends JFrame implements IIG {

	private static final long						serialVersionUID				= 1L;
	public static final String						TITLE							= "IGSAT";
	public static final String						DEFAULT_TITLE_PANE				= "CNF";
	public static final String						DEFAULT_ERROR_POPUP_TITLE		= "ERROR !!";
	public static final String						DEFAULT_QUESTION_POPUP_TITLE	= " ? ";

	private Map<ISessionManager, IGSessionManager>	sessions						= new HashMap<ISessionManager, IGSessionManager>();
	private JTabbedPane								tabbedPane;
	private IController								controller						= null;
	private IGSATJMenuBar							menubar;


	/**
	 * Create an IG for the application, to lunch treatment on different CNF.
	 * 
	 * @param ctrl
	 *            the controller.
	 * @throws HeadlessException
	 */
	public IG(IController ctrl) throws HeadlessException {
		super();
		setController(ctrl);
	}

	/**
	 * Create an IG for the application, to lunch treatment on different CNF. Don't forget to set
	 * the controller.
	 * 
	 * @throws HeadlessException
	 */
	public IG() throws HeadlessException {
		super(TITLE);
		// TODO Auto-generated constructor stub

		tabbedPane = new JTabbedPane(JTabbedPane.SCROLL_TAB_LAYOUT);
		setContentPane(tabbedPane);

		// set the jmenubar
		menubar = new IGSATJMenuBar();
		setJMenuBar(menubar);

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		WindowAdapter wa = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				controller.close();
			}
		};
		addWindowListener(wa);
		pack();
		setVisible(true);
	}


	@Override
	public void closeSession(ISessionManager session) {
		// TODO session
		IGSessionManager tmp = sessions.remove(session);
		if (tmp == null)
			return;

		tabbedPane.remove(tmp);
		if (!tmp.close())
			showErrorPopup(EErrorPopUp.ERROR_CLOSE_IG_SESSION, "error closing : " + tmp);
	}

	@Override
	public IIGSessionManager openSession(CNFUse cnf, ISessionManager manager) {
		// TODO a tester
		if (cnf == null || manager == null)
			return null;

		IGSessionManager session = new IGSessionManager(cnf, manager);

		tabbedPane.addTab(DEFAULT_TITLE_PANE + (sessions.size() + 1), session);
		sessions.put(manager, session);


		// tabbedPane.addChangeListener(this);
		// repaint();
		// update(getGraphics());
		// paintComponents(session.getGraphics());

		// session.revalidate();
		// repaint();
		// tabbedPane.validate();
		tabbedPane.setSelectedComponent(session);
		pack();
		return session;
	}

	@Override
	public void showErrorPopup(EErrorPopUp type, String message) {
		// TODO a tester
		JOptionPane.showMessageDialog(null, type.getPrefix() + "\n" + message + "\n" + type.getSuffix(), DEFAULT_ERROR_POPUP_TITLE,
				JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public void setController(IController ctrl) {
		controller = ctrl;
		menubar.setController(ctrl);
	}

	@Override
	public IController getController() {
		return controller;
	}

	@Override
	public boolean yesNoQuestionPopup(String question) {
		if (JOptionPane.showConfirmDialog(null, "\n" + question + "\n", DEFAULT_QUESTION_POPUP_TITLE, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			return true;
		return false;
	}

}
